package com.itheima.redbaby.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Comment;
import com.itheima.redbaby.engine.ProductEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class ProductCommentActivity extends BaseActivity implements OnClickListener{
	private final int PAGENUM = 10;
	
	private CommentAdapter adapter;
	private int pId;
	private int page = 1; //默认第一页
	private List<Comment> list_comment = new ArrayList<Comment>();

	private TextView comment_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prod_comments_activity);
		
		pId = getIntent().getIntExtra("pId", 0);
		
		init();
		getData();
	}
	
	private void init() {
		TextView titleBack = (TextView) findViewById(R.id.titleBack);
		comment_title = (TextView) findViewById(R.id.comment_title);
		ListView listComments = (ListView) findViewById(R.id.listComments);
		
		titleBack.setOnClickListener(this);
		
		adapter = new CommentAdapter();
		listComments.setAdapter(adapter);
		listComments.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if(scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					//如果到头了
					if(view.getLastVisiblePosition() == list_comment.size() - 1) {
						//设置页数加1
						page++;
						//重新获取数据
						getData();
					}
				}
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {}
		});
	}

	private void getData() {
		new MyHttpTask() {
			@Override
			protected Object doInBackground(Void... params) {
				ProductEngine productEngine = DaoAndEngineFactory.getImpl(ProductEngine.class);
				Map<String, Object> resMap = productEngine.getProductComment(pId, page, PAGENUM);
				
				return resMap;
			}
			@Override
			@SuppressWarnings("unchecked")
			protected void onPostExecute(Object result) {
				Map<String, Object> resMap = (Map<String, Object>) result;
				List<Comment> list = (List<Comment>) resMap.get("comments");
				int comment_number = (Integer) resMap.get("comment_number");
				list_comment.addAll(list);
				
				adapter.notifyDataSetChanged();
				comment_title.setText("购买评论 ( " + comment_number + " ) ");
			};
		}.executeHttp();
	}
	
	class CommentAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			if(list_comment == null || list_comment.size() == 0)
				return 0;
			return list_comment.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null) {
				convertView = View.inflate(ProductCommentActivity.this, R.layout.prod_comments_items, null);
				CommentHolder holder = new CommentHolder();
				holder.textComTitle = (TextView) convertView.findViewById(R.id.textComTitle);
				holder.textClothesDetail = (TextView) convertView.findViewById(R.id.textClothesDetail);
				holder.textComFrom = (TextView) convertView.findViewById(R.id.textComFrom);
				holder.textComDate = (TextView) convertView.findViewById(R.id.textComDate);
				convertView.setTag(holder);
			}
			CommentHolder holder = (CommentHolder) convertView.getTag();
			Comment comment = list_comment.get(position);
			holder.textComTitle.setText(comment.getTitle());
			holder.textClothesDetail.setText(comment.getContent());
			holder.textComFrom.setText(comment.getUsername());
			holder.textComDate.setText(comment.getTime());
			
			if(position % 2 != 0) {
				convertView.setBackgroundColor(0x55CCCCCC);
			} else {
				convertView.setBackgroundColor(Color.WHITE);
			}
			return convertView;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
	}

	class CommentHolder {
		TextView textComTitle;
		TextView textClothesDetail;
		TextView textComFrom;
		TextView textComDate;
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.titleBack:
			finish();
			break;
		}
	}
}
